<?php

class RedisManager
{
    private $redis;
    private $config;
    private static $instance = null;
    private function __construct(array $config)
    {
        $this->config = $config;
        $this->connect();
    }
    public static function getInstance(array $config)
    {
        if (self::$instance === null) {
            self::$instance = new self($config);
        }
        return self::$instance;
    }
    public function connect()
    {
        $this->redis = new Redis();
        $this->redis->connect($this->config['host'] ?? '127.0.0.1', $this->config['port'] ?? 6379);
        if (isset($this->config['password']) && !empty($this->config['password'])) {
            $this->redis->auth($this->config['password']);
        }
        $this->redis->select($this->config['db'] ?? 0);
    }

    public function __call($name, $arguments)
    {
        return $this->redis->{$name}(...$arguments);
    }
    public function __destruct()
    {
        $this->redis->close();
    }
    public function __clone()
    {
        throw new \Exception('Clone is not allowed');
    }
    public function __wakeup()
    {
        throw new \Exception('Unserialize is not allowed');
    }
}
